package com.xc.h05.controller.changlongfei;

import com.xc.h05.entity.Coursemark;
import com.xc.h05.entity.Coursepurchase;
import com.xc.h05.entity.Coursereply;
import com.xc.h05.entity.UserCourse;
import com.xc.h05.mapper.CoursepurchaseMapper;
import com.xc.h05.mapper.UserCourseMapper;
import com.xc.h05.service.changlongfei.CourseDetailsService;
import com.xc.h05.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//课程详情页面，包含其下四个小模块
@RestController
@RequestMapping("/courseDetails")
public class CourseDetailsController {
//    课程详情主页面+课程简介页面
    @Autowired
    private CourseDetailsService courseDetailsService;
    @GetMapping("/getCourseDetails")
    public R getCourseDetails(Long id){
        return R.ok(courseDetailsService.getCourseDetails(id));
    }


//    名师介绍页面
    @GetMapping("/getConsultById")
    public R getConsultById(Long id){
        return R.ok(courseDetailsService.getConsultById(id));
    }
    //查看证书，咨询师详情，咨询预约  没写，到时候传入ID



//    课程评价界面

    @GetMapping("/getCoursemarkById")
    public R getCoursemarkById(Long id){
        return R.ok(courseDetailsService.getCoursemarkById(id));
    }

    @PostMapping("/addCoursemark")
    public R addCoursemark(Coursemark coursemark){
        int i = courseDetailsService.addCoursemark(coursemark);
        if(i>0){
            return R.ok("添加成功");
        }else{
            return R.ok("添加失败");
        }
    }

//    课堂交流界面
    //最新发表
    @GetMapping("/getCoursetalkByIdZuiXin")
    public R getCoursetalkByIdZuiXin(Long id){
        return R.ok(courseDetailsService.getCoursetalkByIdZuiXin(id));
    }

    //最后回复
    @GetMapping("/getCoursetalkByIdZuiHou")
    public R getCoursetalkByIdZuiHou(Long id){
        return R.ok(courseDetailsService.getCoursetalkByIdZuiHou(id));
    }

    //回复数量
    @GetMapping("/getCoursetalkByIdShuLiang")
    public R getCoursetalkByIdShuLiang(Long id){
        return R.ok(courseDetailsService.getCoursetalkByIdShuLiang(id));
    }
    //添加回复
    @PostMapping("/addCoursereply")
    public R addCoursereply(Coursereply record){
        int i = courseDetailsService.addCoursereply(record);
        if(i>0){
            return R.ok("添加成功");
        }else{
            return R.ok("添加失败");
        }
    }

//    购买
    @Autowired
    private CoursepurchaseMapper coursepurchaseMapper;
    @PostMapping("/buyCourse")
    public R buyCourse(Coursepurchase coursepurchase){
        int i = coursepurchaseMapper.insert(coursepurchase);
        if(i>0){
            return R.ok("购买成功");
        }else{
            return R.ok("购买失败");
        }
    }
//    收藏
    @Autowired
    private UserCourseMapper userCourseMapper;
    @PostMapping("/collectCourse")
    public R collectCourse(UserCourse userCourse){
        int i = userCourseMapper.insert(userCourse);
        if(i>0){
            return R.ok("收藏成功");
        }else{
            return R.ok("收藏失败");
        }
    }
//    取消收藏
    @DeleteMapping("/cancelCollectCourse")
    public R cancelCollectCourse(Long userId,Long courseId){
        int i = userCourseMapper.deleteByUserIdAndCourseId(userId, courseId);
        if(i>0){
            return R.ok("取消收藏成功");
        }else{
            return R.ok("取消收藏失败");
        }
    }


}
